<?php

class Quad_Orm_Writer {

	public function __construct($table, $metadata) {
		$this->table = $table;
		$this->columns = $metadata['columns'];
		$this->pk = $metadata['pk'];
		$this->serial = $metadata['serial'];
	}
	
	public function getDefinitionPhp($definitionClassName, $definitionBaseName) {
		
		$columns = array();
		foreach ($this->columns as $col) {
			$columns[] = "'$col'";
		}
		$colList = join(',', $columns);
		
		$pks = array();
		foreach ($this->pk as $pk) {
			$pks[] = "'$pk'";
		}
		$pkList = join(',', $pks);
	
		$serial = ($this->serial ? "'$this->serial'" : 'false');
		
		$now = date('n/j/Y g:ia');
		
		$def = "<?php
/**
 * Quad PHP ORM Definition for table $this->table
 * AUTOMATICALLY GENERATED FILE - DO NOT MODIFY
 *
 * @date $now
 * @author (Auto Generated By Quad PHP Library)
 * @licence MIT License - http://code.google.com/p/quadphp/source/browse/trunk/MIT-LICENSE
 * @see http://code.google.com/p/quadphp/wiki/OrmApi
 */
class $definitionClassName extends $definitionBaseName {

\tprotected \$_table = '$this->table';

\tprotected \$_columns = array($colList);

\tprotected \$_pk = array($pkList);

\tprotected \$_serial = $serial;

}
";
		return $def;
	}
	
	public function getAppPhp($appClassName, $definitionClassName) {

		$colNames = join(', ',$this->columns);
		$pkNames = join('+',$this->pk);
		$serial = ($this->serial ? $this->serial : '{none}');
		$now = date('n/j/Y g:ia');
		
		$app = "<?php
/**
 * Quad PHP ORM Class
 *
 * Table:       $this->table
 * Columns:     $colNames
 * Primary Key: $pkNames
 * Serial:      $serial
 *
 * @date $now 
 * @author (Auto Generated By Quad PHP Library)
 * @licence MIT License - http://code.google.com/p/quadphp/source/browse/trunk/MIT-LICENSE
 * @see http://code.google.com/p/quadphp/wiki/OrmApi
 */
class $appClassName extends $definitionClassName {

	/* Add properties and methods here */

}
";

		return $app;
	}
	
}
